/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iteracon.generator.visitor;

import iteracon.generator.RangeBase;
import iteracon.generator.Rng;
import iteracon.generator.iterator.FromZeroToIter;
import iteracon.generator.iterator.FromZeroToListIter;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * range generator from zero to another integer
 * @author martin
 */
public class FromZeroTo extends RangeBase{
 
  private final int _size;
  /**
   * constructor
   * @param size as int
   */
  private FromZeroTo(int size){
    _size=size;
  }
  /**
   * static factory
   * @param size as int
   * @return range generator as List
   */
  public static Rng make(int size){
    return new FromZeroTo(size);
  }
  
  
  @Override
  public Iterator<Integer> iterator() {
    return new FromZeroToIter(_size);
  }

  @Override
  public ListIterator<Integer> listIterator() {
    return new FromZeroToListIter(_size,0);
  }

  @Override
  public ListIterator<Integer> listIterator(int index) {
    return new FromZeroToListIter(_size,index);
  }

  @Override
  public List<Integer> subList(int fromIndex, int toIndex) {
    if (fromIndex<0 || toIndex >= size())
      throw new IndexOutOfBoundsException();
    return FromTo.make(fromIndex, toIndex);  
  }
  

  @Override
  public int size() {
    return _size;
  }

  @Override
  public Integer get(int index) {
    //if (index<size())
      return index;
    //throw new IndexOutOfBoundsException();
  }
  
  @Override
  public boolean contains(Object o) {
    //optimization
    if (!(o instanceof Integer))
      return false;
    return ((Integer)o) < _size && ((Integer)o) >= 0;
  }
  
  @Override
  public int indexOf(Object o) {
    //optimization
    if (!(o instanceof Integer) || ((Integer)o) >= _size || ((Integer)o) < 0)
      return -1;
    return ((Integer)o);
  }

  @Override
  public int lastIndexOf(Object o) {
    //optimization
    return indexOf(o);
  }

  @Override
  public int from() {
    return 0;
  }

  @Override
  public int to() {
    return _size;
  }

  @Override
  public int step() {
    return 1;
  }
}
